#include "Color.h"


NS_B2D_BEGIN
	
const Color Color::White		(1.f, 1.f, 1.f);
const Color Color::Black		(0.f, 0.f, 0.f);
const Color Color::Red			(1.f, 0.f, 0.f);
const Color Color::Green		(0.f, 1.f, 0.f);
const Color Color::Blue			(0.f, 0.f, 1.f);
const Color Color::Transparent	(0.f, 0.f, 0.f, 0.0f);

Color::Color(void)
{
	set(0.f,0.f,0.f,0.f);
}

Color::Color(float r, float g, float b, float a)
{
	set(r, g, b, a);
}

bool Color::operator == (const Color& c) const
{
	return c.r == r && c.g == g && c.b == b && c.a == a;
}

bool Color::operator != (const Color& c) const
{
	return !(*this == c);
}

float Color::operator [] (size_t i) const
{
	B2D_ASSERT(i < 4);
	return *(&r + i);
}

float& Color::operator [] (size_t i)
{
	B2D_ASSERT(i < 4);
	return *(&r + i);
}

float* Color::getPtr(void)
{
	return &r;
}

const float* Color::getPtr(void) const
{
	return (const float*)&r;
}

void Color::set(float r, float g, float b, float a)
{
	this->r = r;
	this->g = g;
	this->b = b;
	this->a = a;
}

void Color::set(uint8 r, uint8 g, uint8 b, uint8 a)
{
	static const float one_over_255 = (1.f/255.f);
	this->r = one_over_255 * r;
	this->g = one_over_255 * g;
	this->b = one_over_255 * b;
	this->a = one_over_255 * a;
}

void Color::setRGBA(uint32 rgba)
{
	uint8* vals = (uint8*)&rgba;
	set(vals[0], vals[1], vals[2], vals[3]);
}

uint32 Color::getRGBA(void) const
{
	uint32 color = uint8(r*255) << 24 | uint8(g*255) << 16 | uint8(b*255) << 8 | uint8(a*255);
	return color;
}


NS_B2D_END